
import tools.sqlite_tool as sqlite_tool
import tools.mode_db as mode_db
from tools.utils import dic_result

# 获取机器人信息，rpa_type为空则获取全部
def get_rpa_info(data):
    try:
        rpa_type = data.get("rpa_type", "")
        name = data.get("name", "")

        sql = sqlite_tool.SQLiteTool()
        base_query = """SELECT ri.rpa_type ,ri.name,ri.version ,ri.status ,0 ,0 ,0 ,0
                    from  rpa_info ri where ri.is_del = 0 """
        # 动态构建 WHERE 子句
        conditions = []
        params = []
        if rpa_type:
            conditions.append(" and ri.rpa_type = ? ")
            params.append(rpa_type)
        if name:
            conditions.append(" and ri.name like ? ")
            params.append(f"%{data.get('name')}%")
        # 将条件添加到查询语句中
        if conditions:
            base_query += " " + " ".join(conditions)
        # 排序条件
        orderStr = " order by ri.rpa_type ASC "
        base_query += orderStr

        # 查询数据
        res = sql.query(base_query, params)
        obj_list = [mode_db.RpaInfo(*row) for row in res]
        return dic_result("0", "success", obj_list)
    except Exception as e:
        print("发生了一个错误:", e)
        return dic_result("1", f"操作失败:{str(e)}", "")
    finally:
        sql.close()


# if __name__ == "__main__":
#     result = get_run_logs_forTree({'group_id':'2e0421e828874e859e344af603bd9be0'})
#     print("result--", result)
